package com.ideafactory.client.utils;

import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

import com.ideafactory.client.ybselfservice.R;
import com.ideafactory.client.ybservice.APP;

import java.io.File;
import java.math.BigDecimal;

/**
 * Created by LiuShao on 2016/4/6.
 */
public class SdCardUtils {

    /**
     * 判断sdcard是否可用
     * @return true为可用，否则为不可用
     */
    public static boolean sdCardIsAvailable() {
        String status = Environment.getExternalStorageState();
        if (!status.equals(Environment.MEDIA_MOUNTED))
            return false;
        return true;
    }

    public static String getAvailaleSize(String path) {
        StatFs stat = new StatFs(path);    /*获取block的SIZE*/
        long blockSize = stat.getBlockSize();    /*空闲的Block的数量*/
        long availableBlocks = stat.getAvailableBlocks();   /* 返回bit大小值*/
        long totalBlocks = stat.getBlockCount();
        long uedBlocks = totalBlocks - availableBlocks;
        return "已用:"+ Formatter.formatFileSize(APP.getAPP(), uedBlocks * blockSize) + "/可用:"+Formatter.formatFileSize(APP.getAPP(),totalBlocks * blockSize);
    }


    public static String getMemoryTotalSize(){
        File innerCardFile = Environment.getExternalStorageDirectory();
        long totalSize = innerCardFile.getTotalSpace();
        return String.valueOf(totalSize/1024/1024);
    }

    public static String getMemoryUsedSize(){
        File innerCardFile = Environment.getExternalStorageDirectory();
        long totalSize = innerCardFile.getTotalSpace();
        long freeSize = innerCardFile.getFreeSpace();
        long usedSize = totalSize - freeSize;
        return String.valueOf(usedSize / 1024 / 1024);
    }


    public static final String[] extsdsNoUsb = new String[]{"extsd","external_sd"};

    public static String getNoUsbSdcardPath() {
        for (String string : extsdsNoUsb) {
            File file = new File("mnt/" + string);
            if (file.exists()) {
                if (file.canExecute() && file.canWrite() && file.canRead()) {
                    return file.getPath();
                }
            }
        }
        return "";
    }





    public static String getSDDiskCon () {
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            double blockSize = sf.getBlockSize();
            double blockCount = sf.getBlockCount();
            double availCount = sf.getAvailableBlocks();

            Double level = (availCount * blockSize / 1024);
            Double all = (blockSize * blockCount / 1024);

            Double use = all - level;

            Double useDou = Double.valueOf(use / 1024 / 1024);
            Double levelDou = Double.valueOf(level / 1024/ 1024);
            Double allDou = Double.valueOf(all / 1024/ 1024);

            BigDecimal useB = new BigDecimal(useDou);
            double useF = useB.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            BigDecimal levelB = new BigDecimal(levelDou);
            double levelF = levelB.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            BigDecimal allB = new BigDecimal(allDou);
            double allF = allB.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            String useStr = useF+"G";
            if (useF < 1) {
                useF = useF * 1024;
                useStr = useF+"M";
            }
            String levelStr = levelF+"G";
            if (levelF < 1) {
                levelF = levelF * 1024;
                levelStr = levelF+"M";
            }
            String allStr = allF+"G";
            if (allF < 1) {
                allF = allF * 1024;
                allStr = allF+"M";
            }
            return APP.getAPP().getResources().getString(R.string.used)+(useStr) + "/"+APP.getAPP().getResources().getString(R.string.Available) + allStr;
        } else {
            return "";
        }
    }








}
